package com.woniu.service.impl;

import com.woniu.entity.Interview1;
import com.woniu.mapper.InterviewDao;
import com.woniu.pojo.Interview;
import com.woniu.pojo.InterviewExample;
import com.woniu.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class InterviewServiceImpl implements InterviewService {

    @Autowired
    private InterviewDao interviewDao;

    @Override
    public void add(Interview interview) {
        interviewDao.insert(interview);
    }

    @Override
    public void deleteById(Integer viewId) {
        interviewDao.deleteByPrimaryKey(viewId);
    }

    @Override
    public Interview1 findOne(Integer viewId) {
        return interviewDao.selectByPrimaryKey1(viewId);
    }

    @Override
    public void updateById(Interview interview) {
        interviewDao.updateByPrimaryKey(interview);
    }

    @Override
    public List<Interview1> findAll(Interview interview) {
        InterviewExample example = new InterviewExample();
        InterviewExample.Criteria criteria = example.createCriteria();
        if (interview != null){
            if (interview.getStudentId() != null && !interview.getStudentId().equals("")) {
                criteria.andStudentIdEqualTo(interview.getStudentId());
            }
            if (interview.getCompanyId() != null && !interview.getCompanyId().equals("")) {
                criteria.andCompanyIdEqualTo(interview.getCompanyId());
            }
        }
        return interviewDao.selectByExample1(example);
    }

    @Override
    public void deleteByExample(List<Integer> interviewIds) {
        InterviewExample example = new InterviewExample();
        example.createCriteria().andViewIdIn(interviewIds);
        interviewDao.deleteByExample(example);
    }

    @Override
    public Interview findViewResult(Integer studentId) {
        InterviewExample example = new InterviewExample();
        example.createCriteria().andStudentIdEqualTo(studentId);
        List<Interview> interviewList = interviewDao.selectByExample(example);
        return interviewList.size() == 0 ? null : interviewList.get(0);
    }

    @Override
    public List<Interview> findClassViewResults(Integer classId) {
        return interviewDao.findClassViewResults(classId);
    }
}
